import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;


import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Servlet implementation class Search
 */
public class Search extends HttpServlet {
	private static final long serialVersionUID = 1L;  
    public Search() {
        super();        
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		doSearch(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		doSearch(request,response);
	}
	public void doSearch(HttpServletRequest request, HttpServletResponse resp) throws IOException 
	{
		try{
		Class.forName("org.hsqldb.jdbcDriver");
		Connection connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/xdb", "sa", "");
		PreparedStatement ps=connection.prepareStatement("select * from usr where name like ?");
		String nameToSearch=request.getParameter("key");
		ps.setString(1,nameToSearch+"%");
		ResultSet rs=ps.executeQuery();
		request.setAttribute("searchResult", rs);
		RequestDispatcher rd = getServletContext().getRequestDispatcher("/show.jsp"); 
		rd.forward(request, resp);
		
		}catch(Exception ex)
		{
			//.println(ex.printStackTrace(s) ex.toString());
			ex.printStackTrace(resp.getWriter());
			ex.printStackTrace();
		}

	}

}
